/**
 *  以下两个函数解决了dicom 解析出现中文的情况。
 * @param byteArray
 * @param position
 * @param length
 * @returns {*[]}
 */
function readFixedString(byteArray, position, length) {
    if (length < 0) {
        throw 'dicomParser.readFixedString - length cannot be less than 0'
    }
    if (position + length > byteArray.length) {
        throw 'dicomParser.readFixedString: attempt to read past end of buffer'
    }
    let byteArr = []
    for (let i = 0; i < length; i++) {
        let byte = byteArray[position + i]
        byteArr.push(byte)
    }
    return byteArr
}

function stringChinese(tag, data) {
    return new Promise((resolve, reject) => {
        let element = data.elements[tag]
        if (element && element.length > 0) {
            let fixedString = readFixedString(data.byteArray, element.dataOffset, element.length)
            let array = new Uint8Array(fixedString)
            let blob = new Blob([array])
            let reader = new FileReader()
            reader.readAsText(blob, 'utf8')
            reader.onload = (e) => {
                resolve(e.target.result)
            }
        }
    })

}


export {
    stringChinese
}